package com.usian.controller;

import cn.hutool.db.sql.Order;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.entity.*;
import com.usian.service.CourseService;
import com.usian.service.FenginEmpService;
import com.usian.service.SumcouService;
import com.usian.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈宇轩
 * @version 1.0
 * @date 2023/8/23 21:13
 */

@RequestMapping("/course")
@RestController
public class CourseController {
    @Autowired
    private CourseService comService;
    @Autowired
    private FenginEmpService fenginEmpService;
    @Autowired
    private SumcouService sumcouService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/findList")
    public List<Emp> findList(){
        return fenginEmpService.findList();
    }
    @GetMapping("/findAll")
    public ResultResponse findAll(CourseDTO courseDTO){
        try {
            PageHelper.startPage(courseDTO.getPageNum(),courseDTO.getPageSize());
            List<Course> list=comService.findAll(courseDTO);
            PageInfo<Course> pageInfo = new PageInfo<>(list);
            return ResultResponse.Success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("/findAllCourse")
    public ResultResponse findAllCourse(){
        try {
            Integer ss=comService.findAllCourse();
            return ResultResponse.Success(ss);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("syCou")
    public ResultResponse syCou(){
        List<Course> list = comService.syCou();
        return ResultResponse.Success(list);
    }
    @GetMapping("syCou1")
    public ResultResponse syCou1(){
        List<Course> list = comService.syCou1();
        return ResultResponse.Success(list);
    }
    @GetMapping("dz1")
    public ResultResponse dz1(@RequestParam Integer couId){
        LambdaQueryWrapper<Course> w = new LambdaQueryWrapper<>();
        w.eq(Course::getId, couId);
        Course cou = comService.getOne(w);
        String mebers =  redisTemplate.opsForValue().get("sumerID").toString();
        Sumer mebers1 = JSON.parseObject(mebers, Sumer.class);
        Object o =  redisTemplate.opsForValue().get(couId + mebers1.getId());
        if (o==null){
            cou.setDz(cou.getDz()+1);
            redisTemplate.opsForValue().set(couId+mebers1.getId(),cou.getDz());
            comService.updateById(cou);
            return ResultResponse.Success("感谢");
        }else {
            cou.setDz(cou.getDz()-1);
            redisTemplate.delete(couId+mebers1.getId());
            comService.updateById(cou);
            return ResultResponse.Success("抱歉");
        }
//        cou.setDz(cou.getDz()+1);
//        comService.updateById(cou);
//        return ResultResponse.Success("感谢");
    }
    @GetMapping("dz2")
    public ResultResponse dz2(@RequestParam Integer couId){
        LambdaQueryWrapper<Course> w = new LambdaQueryWrapper<>();
        w.eq(Course::getId, couId);
        Course cou = comService.getOne(w);
        String mebers =  redisTemplate.opsForValue().get("sumerID").toString();
        Sumer mebers1 = JSON.parseObject(mebers, Sumer.class);
        Object o =  redisTemplate.opsForValue().get(couId + mebers1.getId());
        if (o==null){
            cou.setDz(cou.getDz()+1);
            redisTemplate.opsForValue().set(couId+mebers1.getId(),cou.getDz());
            comService.updateById(cou);
            return ResultResponse.Success("感谢");
        }else {
            cou.setDz(cou.getDz()-1);
            redisTemplate.delete(couId+mebers1.getId());
            comService.updateById(cou);
            return ResultResponse.Success("抱歉");
        }
    }
    @GetMapping("dz3")
    public ResultResponse dz3(@RequestParam Integer couId){
        LambdaQueryWrapper<Course> w = new LambdaQueryWrapper<>();
        w.eq(Course::getId, couId);
        Course cou = comService.getOne(w);
        String mebers =  redisTemplate.opsForValue().get("sumerID").toString();
        Sumer mebers1 = JSON.parseObject(mebers, Sumer.class);
        Object o =  redisTemplate.opsForValue().get(couId + mebers1.getId());
        if (o==null){
            cou.setDz(cou.getDz()+1);
            redisTemplate.opsForValue().set(couId+mebers1.getId(),cou.getDz());
            comService.updateById(cou);
            return ResultResponse.Success("感谢");
        }else {
            cou.setDz(cou.getDz()-1);
            redisTemplate.delete(couId+mebers1.getId());
            comService.updateById(cou);
            return ResultResponse.Success("抱歉");
        }
    }
    @GetMapping("dz4")
    public ResultResponse dz4(@RequestParam Integer couId){
        LambdaQueryWrapper<Course> w = new LambdaQueryWrapper<>();
        w.eq(Course::getId, couId);
        Course cou = comService.getOne(w);
        String mebers =  redisTemplate.opsForValue().get("sumerID").toString();
        Sumer mebers1 = JSON.parseObject(mebers, Sumer.class);
        Object o =  redisTemplate.opsForValue().get(couId + mebers1.getId());
        if (o==null){
            cou.setDz(cou.getDz()+1);
            redisTemplate.opsForValue().set(couId+mebers1.getId(),cou.getDz());
            comService.updateById(cou);
            return ResultResponse.Success("感谢");
        }else {
            cou.setDz(cou.getDz()-1);
            redisTemplate.delete(couId+mebers1.getId());
            comService.updateById(cou);
            return ResultResponse.Success("抱歉");
        }
    }
    @GetMapping("/setRedisCouId")
    public ResultResponse setRedisCouId(Integer couId){
        redisTemplate.opsForValue().set("课程ID",couId);
        return ResultResponse.Success("111111");
    }
    @GetMapping("alreadyLearn")
    public ResultResponse alreadyLearn(){
        String member = (String)redisTemplate.opsForValue().get("sumerID");
        Sumer member1 = JSON.parseObject(member, Sumer.class);
        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");
        LambdaQueryWrapper<Sumcou> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sumcou::getSumerId,member1.getId()).eq(Sumcou::getCouId, couId);
        List<Sumcou> memberCatalogueList = sumcouService.list(wrapper);
        List<Integer> collect = memberCatalogueList.stream().map(memberCatalogue -> {
            return memberCatalogue.getCatalogueId();
        }).collect(Collectors.toList());
        return  ResultResponse.Success(collect);
    }
    @GetMapping("getCatalogueAltogether")
    public ResultResponse getCatalogueAltogether(){
        String member = (String)redisTemplate.opsForValue().get("sumerID");
        Sumer member1 = JSON.parseObject(member, Sumer.class);
        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");
        LambdaQueryWrapper<Sumcou> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sumcou::getSumerId,member1.getId()).eq(Sumcou::getCouId,couId);
        List<Sumcou> memberCatalogueList = sumcouService.list(wrapper);
        int size = memberCatalogueList.size();
        return ResultResponse.Success(size);
    }
    @GetMapping("setAlreadyLearn")
    public ResultResponse setAlreadyLearn(Integer[] catalugueIds){
        String member = (String)redisTemplate.opsForValue().get("sumerID");
        Sumer member1 = JSON.parseObject(member, Sumer.class);

        Integer couId = (Integer) redisTemplate.opsForValue().get("课程ID");

        LambdaQueryWrapper<Sumcou> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sumcou::getSumerId, member1.getId()).eq(Sumcou::getCouId, couId);
        sumcouService.remove(wrapper);

        for (Integer catalugueId : catalugueIds) {
            Sumcou m = new Sumcou();
            m.setSumerId(Integer.parseInt(member1.getId()+""));
            m.setCatalogueId(catalugueId);
            m.setCouId(couId);
            sumcouService.save(m);
        }
        return ResultResponse.Success("22222222");

    }
}
